﻿using InfoPathDebugger;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Office.InfoPath;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;
using System.Xml.XPath;

namespace XPathHelperTest
{
    
    
    /// <summary>
    ///This is a test class for XPathHelperTest and is intended
    ///to contain all XPathHelperTest Unit Tests
    ///</summary>
    [TestClass()]
    public class XPathHelperTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for GetDatasourceLeafNodesXPath
        ///</summary>
        [TestMethod()]
        public void GetDatasourceLeafNodesXPathTest()
        {
            System.IO.StringReader sr = new System.IO.StringReader(dsFileSample);
            System.Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Load(sr);

            XPathNavigator nav = xDoc.Elements().First().CreateNavigator();

            List<string> allXPaths = XPathHelper.GetDatasourceLeafNodesXPath(nav);
            Assert.AreEqual(7, allXPaths.Count);

            bool found = allXPaths.Exists(delegate(string str)
            {
                return str == "/my:assetTracking/my:employee/my:employeeName";
            });

            Assert.IsTrue(found);

            found = allXPaths.Exists(delegate(string str)
            {
                return str == "/my:assetTracking/my:assets/my:asset/my:assetID";
            });

            found = allXPaths.Exists(delegate(string str)
            {
                return str == "/my:assetTracking/my:assets/my:asset/my:assetDescription";
            });

            found = allXPaths.Exists(delegate(string str)
            {
                return str == "/my:assetTracking/my:assets/my:asset/my:assetMake";
            });

            found = allXPaths.Exists(delegate(string str)
            {
                return str == "/my:assetTracking/my:assets/my:asset/my:assetModel";
            });

            found = allXPaths.Exists(delegate(string str)
            {
                return str == "/my:assetTracking/my:assets/my:asset/my:assetSerialNumber";
            });

            Assert.IsTrue(found);
        }

        /// <summary>
        ///A test for CreateXPathString
        ///</summary>
        [TestMethod()]
        public void CreateXPathStringTest()
        {
            System.IO.StringReader sr = new System.IO.StringReader(dsFileSample);
            System.Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Load(sr);

            XPathNavigator nav = xDoc.Elements().First().CreateNavigator();
            nav.MoveToChild("assets", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");
            nav.MoveToChild("asset", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");
            nav.MoveToChild("assetID", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27");

            string xpathStr = XPathHelper.CreateXPathString(nav);
            Assert.AreEqual("/my:assetTracking/my:assets/my:asset/my:assetID", xpathStr);
        }

        private string dsFileSample = @"<?xml version=""1.0"" encoding=""UTF-8""?>
<?mso-infoPathSolution name=""urn:schemas-microsoft-com:office:infopath:SrcFileTest:-myXSD-2005-10-21T21-12-27"" href=""manifest.xsf"" solutionVersion=""1.0.0.7"" productVersion=""12.0.0"" PIVersion=""1.0.0.0"" ?>
<?mso-application progid=""InfoPath.Document"" versionProgid=""InfoPath.Document.2""?>
<my:assetTracking xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xhtml=""http://www.w3.org/1999/xhtml"" xmlns:my=""http://schemas.microsoft.com/office/infopath/2003/myXSD/2005-10-21T21:12:27"" xmlns:xd=""http://schemas.microsoft.com/office/infopath/2003"">
	<my:employee>
		<my:employeeName></my:employeeName>
		<my:employeeDepartment></my:employeeDepartment>
	</my:employee>
	<my:assets>
		<my:asset>
			<my:assetID></my:assetID>
			<my:assetDescription></my:assetDescription>
			<my:assetMake></my:assetMake>
			<my:assetModel></my:assetModel>
			<my:assetSerialNumber></my:assetSerialNumber>
		</my:asset>
	</my:assets>
</my:assetTracking>";


    }
}
